/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import com.opensymphony.xwork2.ActionSupport;

import java.util.Set;
import pojo.Category;
import pojo.User;
import pojo.Role;
import service.ICategoryService;
import service.IUserService;
/**
 *
 * @author administrator
 */
public class CategoryActionAdmin extends ActionSupport {
    private ICategoryService<Category> categoryService;
    private IUserService<User> userService;
    private Category category;
    private int categoryID;
    private String categoryName;
    private String uni;
    private int type;

    public IUserService<User> getUserService() {
        return userService;
    }

    public void setUserService(IUserService<User> userService) {
        this.userService = userService;
    }

    public String getUni() {
        return uni;
    }

    public void setUni(String uni) {
        this.uni = uni;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getCategoryID() {
        return categoryID;
    }

    public void setCategoryID(int categoryID) {
        this.categoryID = categoryID;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }



    public ICategoryService<Category> getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(ICategoryService<Category> categoryService) {
        this.categoryService = categoryService;
    }

    public String setting()
    {
        this.category = this.categoryService.getCategory(this.categoryID);
        return "setting";
    }

    public String delete()
    {
        this.category = this.categoryService.getCategory(this.categoryID);
        User user = this.userService.findUserByUni(this.uni);
        user.getCategories().remove(this.category);
        this.category.getUsers().remove(user);
        if(user.getRole().getRoleId() == 2)
        {
            Role role = new Role();
            role.setRoleId(1);
            role.setRoleName("student");
            user.setRole(role);
        }
        this.userService.updateUser(user);
        this.categoryService.updateCategory(this.category);
        this.type = 1;
        return "setting";
    }

    public String create()
    {
        this.category = this.categoryService.getCategory(this.categoryID);
        User user = this.userService.findUserByUni(this.uni);
        if(user == null)
            this.type = 3;
        else
        {
            user.getCategories().add(this.category);
            this.category.getUsers().add(user);
            if(user.getRole().getRoleId() == 1)
            {
                Role role = new Role();
                role.setRoleId(2);
                role.setRoleName("moderator");
                user.setRole(role);
            }
            this.userService.updateUser(user);
            this.categoryService.updateCategory(this.category);
            this.type = 2;
        }
        return "setting";
    }

}
